#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 60;
vector<int> g[N];
int fa[N];
void dfs(int rt) // 非递归
{
    stack<int> stk;
    stk.push(rt);
    while (stk.size())
    {
        int x = stk.top();
        stk.pop();
        cout << x << ' ';
        vector<int> vec;
        for (auto &y : g[x])
        {
            if (y == fa[x])
                continue;
            vec.push_back(y);
        }
        // for(auto it = vec.end()-1;it != vec.begin();it--)
        //     stk.push(*it);
        for (int i = (int)vec.size() - 1; i >= 0; i--)
            stk.push(vec[i]);
    }
}
// void dfs(int x) // 递归写法
// {
//     cout << x << ' ';
//     for(auto &y: g[x])
//     {
//         if(y == fa[x])continue;
//         dfs(y);
//     }
// }
void bfs(int rt)
{
    queue<int> q;
    q.push(rt);
    while (q.size())
    {
        int x = q.front();
        q.pop();
        cout << x << ' ';
        for (auto &y : g[x])
        {
            if (y == fa[x])
                continue;
            q.push(y);
        }
    }
}
int main()
{
    int n;
    cin >> n;
    for (int i = 2; i <= n; i++)
    {
        cin >> fa[i];
        g[fa[i]].push_back(i);
    }
    bfs(1);
    cout << endl;
    dfs(1);
    return 0;
}
